package component.button;

import component.util.MaterialColors;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class FlatButton extends JButton implements MouseListener{
    protected String text;
    protected Icon icon;
    protected Color normalColor;
    protected Color hoverColor;
    protected Color pressedColor;
    protected Color normalTextColor;
    protected Color hoverTextColor;
    protected Color pressedTextColor;
    protected boolean selected;
    private boolean hover = false;
    public FlatButton(String text, Icon icon) {
        this(text,icon, MaterialColors.GRAY_800, MaterialColors.GRAY_700,MaterialColors.GRAY_600,MaterialColors.WHITE,MaterialColors.WHITE,MaterialColors.BLUE_400);
    }

    public FlatButton(String text, Icon icon, Color normalColor, Color hoverColor, Color pressedColor, Color normalTextColor, Color hoverTextColor, Color pressedTextColor) {
        super(text,icon);
        this.text = text;
        this.icon = icon;
        this.normalColor = normalColor;
        this.hoverColor = hoverColor;
        this.pressedColor = pressedColor;
        this.normalTextColor = normalTextColor;
        this.hoverTextColor = hoverTextColor;
        this.pressedTextColor = pressedTextColor;
        init(text,icon);
    }

    @Override
    protected void init(String text, Icon icon) {
        super.init(text, icon);
        this.setBorderPainted(false);
        this.setFocusPainted(false);
        this.setBackground(normalColor);
        this.setForeground(normalTextColor);
        this.addMouseListener(this);
    }

    public Color getNormalColor() {
        return normalColor;
    }

    public void setNormalColor(Color normalColor) {
        this.normalColor = normalColor;
        this.setBackground(normalColor);
        repaint();
    }

    public Color getHoverColor() {
        return hoverColor;
    }

    public void setHoverColor(Color hoverColor) {
        this.hoverColor = hoverColor;
        repaint();
    }

    public Color getPressedColor() {
        return pressedColor;
    }

    public void setPressedColor(Color pressedColor) {
        this.pressedColor = pressedColor;
        repaint();
    }

    public Color getNormalTextColor() {
        return normalTextColor;
    }

    public void setNormalTextColor(Color normalTextColor) {
        this.normalTextColor = normalTextColor;
        repaint();
    }

    public Color getHoverTextColor() {
        return hoverTextColor;
    }

    public void setHoverTextColor(Color hoverTextColor) {
        this.hoverTextColor = hoverTextColor;
        repaint();
    }

    public Color getPressedTextColor() {
        return pressedTextColor;
    }

    public void setPressedTextColor(Color pressedTextColor) {
        this.pressedTextColor = pressedTextColor;
        repaint();
    }

    public Boolean getSelected() {
        return selected;
    }

    @Override
    public void setSelected(boolean b) {
        super.setSelected(b);
        this.selected = b;
        if (b){
            this.setBackground(pressedColor);
        } else {
            this.setBackground(normalColor);
        }
        this.setForeground(normalTextColor);
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        this.setForeground(this.pressedTextColor);
        this.setBackground(this.pressedColor);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (hover){
            this.setBackground(this.hoverColor);
            this.setForeground(this.hoverTextColor);
        } else {
            this.setForeground(this.normalTextColor);
            this.setBackground(this.normalColor);
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        hover = true;
        if (!selected){
            this.setBackground(this.hoverColor);
            this.setForeground(this.hoverTextColor);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        hover = false;
        if (!selected){
            this.setBackground(this.normalColor);
            this.setForeground(this.normalTextColor);
        }
    }
}
